package com.test.comlection;

import org.w3c.dom.*;

import java.util.EmptyStackException;
import java.util.NoSuchElementException;

public class LinkedStack<E> {
   private final Node<E> head = new Node<E>(null);

   public void push(E element){
       Node<E> node = new Node<>(element);//直接创建新节点
       node.next = head.next;//新节点的下一个变成原本栈顶节点
       head.next = node;//头节点的下一个改成新的节点
   }

   public E pop(){
       if(head.next == null){
           throw new NoSuchElementException("栈为空");
       }
       E e =head.next.element;//先把待出栈元素取出来
       head.next = head.next.next;//直接让头节点的下一个指向下一个的下一个
        return e;
   }
   public boolean isEmpty()
    {
        return head.next == null;
    }
   private static class Node<E>{
       private E element;
       private Node<E> next;

       public Node(E e){
           this.element = e;
       }

   }

    }
